<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->library('session');
				$cookie = array(
					'userid'   => '',
                   'username'  => '',
                   'email'     => '',
                   'logged_in' => TRUE
               );
		//删除Cookie
		$this->session->unset_userdata($cookie);

		$this->load->view('manage/login');
	}

	//用户登录POST
	public function loginaction()
	{

		$this->load->model('Systemuser_model');
		$userName = $this->input->post('UserName');
		$password = $this->input->post('Password');
		$result = $this->Systemuser_model->getuserbyusername($userName);
		
		header("Content-type:text/html;charset=utf-8");
		$this->load->helper('url');
		if($result == null) 
		{
			redirect("/manage/login");
			//echo "用户登录失败！用户名不存在！";
		}
		else 
		{
			$this->load->helper('security');
			$pwd =	do_hash($password, 'md5');
			$pwd = substr($pwd,8,16);

			
			if($pwd== strtolower($result->Password))
			{
				//添加Cookie
				$this->load->library('session');
				$cookie = array(
					'userid'   => $result->UserId,
                   'username'  => $userName,
                   'email'     => $result->UserEmail,
                   'logged_in' => TRUE
               );

				$this->session->set_userdata($cookie);
				
				//跳转
				redirect('/manage');	
			}
			else 
			{
				redirect("/manage/login");
				//echo "用户登录失败！密码不正确！";
			}
			
		}
		
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */